package com.ego.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ego.admin.dao.SysRoleMenuDao;
import com.ego.admin.po.SysRoleMenu;
import com.ego.admin.service.SysRoleMenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: ego
 * @description: 系统角色与系统菜单关联
 * @author: KamTang
 * @create: 2023-02-22 19:18
 **/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenu> implements SysRoleMenuService {
    @Override
    public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
        // 先删除角色与菜单关系
        // 主要是修改时，需要删除原来的角色与菜单关系
        // 添加时，步骤一致
        this.deleteBatch(new Long[]{roleId});

        if(menuIdList.size() == 0){
            return ;
        }

        // 保存角色与菜单关系
        for(Long menuId : menuIdList){
            SysRoleMenu sysRoleMenuEntity = new SysRoleMenu();
            sysRoleMenuEntity.setMenuId(menuId);
            sysRoleMenuEntity.setRoleId(roleId);

            this.save(sysRoleMenuEntity);
        }
    }

    @Override
    public List<Long> queryMenuIdList(Long roleId) {
        return baseMapper.queryMenuIds(roleId);
    }

    @Override
    public void deleteBatch(Long[] roleIds) {
        baseMapper.deleteBatch(roleIds);
    }

    @Override
    public void deleteByMenuId(Long menuId) {
        baseMapper.deleteByMenuId(menuId);
    }
}
